import Vue from 'vue'
import Vuex from 'vuex'
import {getRequest} from "@/api/request";
import de from "element-ui/src/locale/lang/de";

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        routes: [],
        activePath: window.localStorage.getItem("activePath") == null ? "/home" : window.localStorage.getItem("activePath"),
    },
    mutations: {
        SET_ROUTES(state, value) {
            state.routes = value
        },
        SET_ACTIVE_PATH(state, value) {
            window.localStorage.setItem("activePath", value);
            state.activePath = value;
        }
    },
    actions: {
        getRoutes(store, payload) {
            return new Promise(((resolve, reject) => {
                getRequest("/menus/listMenusByRId").then(res => {
                    const {data} = res
                    let syncRoute = fmtRouteFn(data)
                    syncRoute.push(...notFound)
                    store.commit('SET_ROUTES', syncRoute)
                    resolve(syncRoute)
                })
            }))
        },
        logOut(store) {
            return new Promise(((resolve, reject) => {
                window.sessionStorage.removeItem("user");
                store.commit("SET_ROUTES", [])
                resolve()
            }))
        }
    },
    modules: {}
})

const fmtRouteFn = (routes) => {
    let routesArr = []
    routes.forEach(route => {
        let {
            path,
            component,
            name,
            meta,
            iconCls,
            children,
            enable,
        } = route;

        if (children && children instanceof Array) {
            children = fmtRouteFn(children);
        }

        let fmtRoute = {
            path,
            name,
            meta,
            hidden: enable,
            iconCls,
            children,
            component(resolve) {
                // 注意加载Home组件，因为其他的组件都是Home组件的子组件
                if (component.startsWith("Home")) {
                    return import('@/views/' + component)
                } else if (component.startsWith("Emp")) {
                    return import('@/views/emp/' + component )
                } else if (component.startsWith("Per")) {
                    return import('@/views/per/' + component)
                } else if (component.startsWith("Sal")) {
                    return import('@/views/sal/' + component)
                } else if (component.startsWith("Sta")) {
                    return import('@/views/sta/' + component )
                } else if (component.startsWith("Sys")) {
                    require(['@/views/sys/' + component], resolve);
                }
            }
        }
        meta.title = name;
        routesArr.push(fmtRoute)
    })
    return routesArr;
}


const notFound = [
    {
        path: "/404",
        name: "页面丢失",
        hidden: true,
        meta: {
            title: "页面丢失",
        },
        component: resolve => (require(["@/views/common/NotFound"], resolve)),
    }, {
        path: "*", // 此处需特别注意置于最底部
        hidden: true,
        redirect: "/404"
    }
]
